﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices;

namespace HML
{
    /// <summary>
    /// 表示在二维平面中定义点的浮点 x 和 y 坐标的有序对。
    /// </summary>
    [ComVisible(true)]
    [Serializable]
    [TypeConverter(typeof(PointFRConverter))]
    public struct PointFR
    {
        public static readonly PointFR Empty;
        private float x;
        private float y;

        [Browsable(false)]
        public bool IsEmpty
        {
            get
            {
                if ((double)this.x == 0.0)
                {
                    return (double)this.y == 0.0;
                }
                return false;
            }
        }

        public float X
        {
            get
            {
                return this.x;
            }
            set
            {
                this.x = value;
            }
        }

        public float Y
        {
            get
            {
                return this.y;
            }
            set
            {
                this.y = value;
            }
        }

        public PointFR(float x, float y)
        {
            this.x = x;
            this.y = y;
        }

        public static PointFR operator +(PointFR pt, Size sz)
        {
            return PointFR.Add(pt, sz);
        }

        public static PointFR operator -(PointFR pt, Size sz)
        {
            return PointFR.Subtract(pt, sz);
        }

        public static PointFR operator +(PointFR pt, SizeF sz)
        {
            return PointFR.Add(pt, sz);
        }

        public static PointFR operator -(PointFR pt, SizeF sz)
        {
            return PointFR.Subtract(pt, sz);
        }

        public static bool operator ==(PointFR left, PointFR right)
        {
            if ((double)left.X == (double)right.X)
            {
                return (double)left.Y == (double)right.Y;
            }
            return false;
        }

        public static bool operator !=(PointFR left, PointFR right)
        {
            return !(left == right);
        }

        public static PointFR Add(PointFR pt, Size sz)
        {
            return new PointFR(pt.X + (float)sz.Width, pt.Y + (float)sz.Height);
        }

        public static PointFR Subtract(PointFR pt, Size sz)
        {
            return new PointFR(pt.X - (float)sz.Width, pt.Y - (float)sz.Height);
        }

        public static PointFR Add(PointFR pt, SizeF sz)
        {
            return new PointFR(pt.X + sz.Width, pt.Y + sz.Height);
        }

        public static PointFR Subtract(PointFR pt, SizeF sz)
        {
            return new PointFR(pt.X - sz.Width, pt.Y - sz.Height);
        }

        public override bool Equals(object obj)
        {
            if (!(obj is PointFR))
            {
                return false;
            }
            PointFR pointF = (PointFR)obj;
            if ((double)pointF.X == (double)this.X && (double)pointF.Y == (double)this.Y)
            {
                return pointF.GetType().Equals(this.GetType());
            }
            return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return "{X=" + X.ToString(CultureInfo.CurrentCulture) + ",Y=" + Y.ToString(CultureInfo.CurrentCulture) + "}";
        }

        /// <summary>
        /// System.Drawing.PointF 转换成 PointFR
        /// </summary>
        /// <returns></returns>
        public static PointFR ConvertFrom(System.Drawing.PointF pointf)
        {
            return new PointFR(pointf.X, pointf.Y);
        }

        /// <summary>
        /// PointFR 转换成 System.Drawing.PointF
        /// </summary>
        /// <returns></returns>
        public PointF ConvertTo()
        {
            return new PointF(x, y);
        }
    }
}
